package com.nikitova.craps3d.model {
//	import com.nikitova.craps3d.constants.Categories;	
//	import com.nikitova.craps3d.constants.ModelGroupNames;	
//	import nl.bigwheelstudio.util.ObjectUtils;	
//	import com.nikitova.craps3d.constants.ModelGenderId;	
//	import com.nikitova.craps3d.view.components.DaeChannelsSerializer;	
//	import com.nikitova.craps3d.view.components.DaeDocumentSerializer;	
//	import com.nikitova.craps3d.dto.CharacterDTO;	
//	import nl.bigwheelstudio.DebugInterests;
	import com.carlcalderon.arthropod.Debug;
	import com.nikitova.craps3d.constants.Notifications;
	import com.nikitova.craps3d.model.vo.ModelVO;

	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.puremvc.as3.multicore.utilities.startupmanager.interfaces.IStartupProxy;

	import flash.events.IOErrorEvent;

	/**	 * @author schranz	 * 	 */	public class ModelsProxy extends EntityProxy implements IStartupProxy 
	{		public static const NAME		:	String	=	'ModelsProxy';
		public static const SRNAME		:	String	=	'ModelsSRProxy';
		
		private var __collada 			: 	DAE; 
		private var __models 			:	XMLList;
		private var __daeDir 			:	String;
		private var __currentModelId 	:	int;
		
		public function ModelsProxy()		{			super(NAME);
			modelsdata			=	new Vector.<ModelVO>;
			
			//create defaults for the geometries
			//createDefaultActiveGeometries();
		}
		
		public function load():void
		{
			var model:XML;
			for each(model in __models)
			{
				__currentModelId = int(model.@id);
				loadCollada(model.@name);
			}
			
			
		}
		//create defaults for the geometries
//		public function createDefaultActiveGeometries():void
//		{
//			__activeGeometries = [];
//			for each(var item:String in ModelGroupNames.names)
//			{
//				if(item == Categories.TORSO || item==Categories.LEGS)
//				{
//					__activeGeometries[item] = "01_01";
//				}
//				else
//				{
//					__activeGeometries[item] = "01";
//				}
//			}
//		}
		
		public function loadCollada(_id : String, _groups : Array = null) : void 
		{
			
//			//createGroupsArray( _groups );
			__collada 						= 	new DAE(false, "collada", false);
			__collada.addEventListener(FileLoadEvent.LOAD_COMPLETE, 			onColladaLoaded);
			__collada.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, 	onColladaMaterialsLoaded);
			__collada.addEventListener(FileLoadEvent.LOAD_ERROR, ioErrorHandler);
			__collada.load( __daeDir + _id + ".dae" );
		}
		
		
		
//		private function createGroupsArray( _groups : Array) : void 
//		{
//			modelsdata[__currentModelId]				=	new ModelVO();
//			for each (var name : String in _groups)
//			{
//				ModelVO(modelsdata[__currentModelId]).groups[name]	=	[];
//			}
//		}

		private function onColladaMaterialsLoaded(event:FileLoadEvent):void
		{
			__collada.removeEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, onColladaMaterialsLoaded);
			//sendNotification(Notifications.COLLADA_MATERIALS_LOADED);
		}
		
		private function onColladaLoaded(event : FileLoadEvent) : void 
		{
			//test();
			
			__collada.removeEventListener(FileLoadEvent.LOAD_COMPLETE, onColladaLoaded);
//			__activeGeometries				=	[];//gtest
			
			
			var modelData	:	ModelVO		=	new ModelVO();
			modelData.id					=	__currentModelId;
			modelData.dae					=	__collada;
////			bundle all children of the collada into group arrays (e.g. Torso, Legs, etc.), based
////			on the childrens' name (minus the digits).
//			for each (var child : DisplayObject3D in __collada.getChildByName("COLLADA_Scene").children)
//			{
//				var myGroupName	:	String 				= 	StringUtils.retrieveWords(child.name);
//				if(myModelData.groups[myGroupName]!= null)
//				{
////					all models are already in the main collada file, so set only the first model of each group to visible (e.g. "Torso01")
//					myModelData.groups[myGroupName].push(child);
//				} 
//			}
//			sendNotification(Notifications.COLLADA_LOADED);
			Debug.log("ModelID "+__currentModelId);
			modelsdata.push(modelData);
			sendLoadedNotification(Notifications.COLLADA_LOADED, NAME, SRNAME);
//			//ObjectUtils.traceObjectDTO(activeGeometries);//gtest
		}
		
		
		private function ioErrorHandler(_e : FileLoadEvent):void
        {
        	Debug.log("Error loading dae file");
        	sendLoadedNotification(Notifications.COLLADA_LOAD_FAILED, NAME, SRNAME);
		}
		
		
		
		public function getModelGroupName ( _model : DisplayObject3D ) : String
		{
//			for (var groupName : String in modelsdata)
//			{
//				for each(var myModel : DisplayObject3D in ModelVO(modelsdata[__currentModelId]).groups[groupName])
//				{
//					if(myModel == _model) return groupName;
//				}
//			}
			return "";
		}
		
		public function getModelVOById( _id : int ) : ModelVO
		{
			for each (var modelVO : ModelVO in modelsdata)
			{
				if(modelVO.id == _id) return modelVO;
			}
			return null;
		}
		
		
		
		public function set modelsdata (_data : Vector.<ModelVO> ) : void
		{
			data	=	_data;
		}
		
		public function get modelsdata() : Vector.<ModelVO>
		{
			return data as Vector.<ModelVO>;
		}
		
		public function set models (_data : XMLList ) : void
		{
			__models	=	_data;
		}
		
		public function set daePath (_dir : String ) : void
		{
			__daeDir	=	_dir;
		}
	}}